#include "class_strategy_grid_5.h"
#include "class_center.h"
#include "class_strategy_position.h"

extern class_center a;
extern lambda_pool pool;


class_strategy_grid_5::class_strategy_grid_5(int idd, std::string dm, double valuee)
{

memset(&tick, 0, sizeof(CThostFtdcDepthMarketDataField) );
name = dm;

std::string str = a.path_str + "/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/grid_5";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/grid_5/"+dm;
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy/grid_5";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

value = valuee;

cc = new class_strategy_position(a.path_str + "/strategy/grid_5/" + dm);


id=idd;
a.tick_event_map[name].insert([this](){ pool.insert([this](){ if(a.flag[50]==1){run_one();} }); },id);
a.log.insert("strategy/grid_5/"+name,"实例化成功,当前仓位" + std::to_string(cc->position));
}

class_strategy_grid_5::~class_strategy_grid_5()
{
a.tick_event_map[name].pop(id);
	a.log.insert("strategy/grid_5/"+name,"析构完成,当前仓位"+std::to_string(cc->position));
delete cc;
}


// 主执行函数
void class_strategy_grid_5::run_one()
{


//清理报单返回值
a.return_erase(*cc);
//同步持仓
a.position_sync(*cc);
//获取到TICK
tick=a.tick_map[name];


//如果处于买入区且仓位不够则
if(tick.AskPrice1 * (cc->position + 2) < (cc->value + value) && cc->return_vector.size() == 0)
{
a.bd_auto(tick.InstrumentID, '0', tick.UpperLimitPrice, 1, *cc);
a.log.insert("strategy/grid_5/" + name +"交易", name + "多开动作" + std::to_string(tick.AskPrice1));
return;
}

if(tick.BidPrice1 * (cc->position - 2) > (cc->value + value) && cc->return_vector.size() == 0)
{
a.bd_auto(tick.InstrumentID, '1', tick.LowerLimitPrice, 1, *cc);
a.log.insert("strategy/grid_5/" + name + "交易", name + "空开动作" + std::to_string(tick.BidPrice1));
return;
}

return;
}
